<!--  -->
<template>
  <div>
    <el-container class="screening-box">
      <el-form :inline="true" class="form" ref="form" label-position="right" label-width="100px">
        <el-header class="screening-head">
          <div class="title">
            <i class="el-icon-search el-icon--left"></i>筛选查询
          </div>
          <div>
            <span class="toggle" v-show="showScreening" @click="showScreening=!showScreening">
              <i class="el-icon-arrow-up el-icon--left"></i>收起筛选
            </span>
            <span class="toggle" v-show="!showScreening" @click="showScreening=!showScreening">
              <i class="el-icon-arrow-down el-icon--left"></i>展开筛选
            </span>
            <el-button class="tlwok-btn" type="primary" @click="getList">查询</el-button>
            <el-button class="tlwok-btn" type="primary" plain @click="searchName = ''">重置</el-button>
          </div>
        </el-header>
        <el-main class="screening-body" v-show="showScreening">
          <el-form-item label="二级类目名称：" label-width="100" prop="userName">
            <el-input v-model.trim="searchName" placeholder="请输入类目名"></el-input>
          </el-form-item>
        </el-main>
      </el-form>
    </el-container>
    <div class="table-box">
      <div class="table-title">
        <span class="title">
          <template v-for="(item , index) in rolePath">
            <el-link type="primary" @click="$router.push(item.path)">{{item.name}}</el-link>
            <span v-if="index != (rolePath.length - 1)">/</span>
          </template>
        </span>
        <div>
          <el-button class="filing-button" type="primary" icon="el-icon-plus" @click="showAddDialog(null)">添加项目</el-button>
        </div>
      </div>
      <div class="list">
        <el-table ref="multipleTable" :data="tableData" style="width: 100%" border :header-cell-style="tableHeaderColor" :cell-style="tableCellStyle">
          <el-table-column prop="name" label="类目名称" width="280"></el-table-column>
          <el-table-column prop="filesCount" label="文件数量" width="280"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link type="primary" @click="toNext(scope.row.id)">打开</el-link>
              <el-link type="primary" @click="showAddDialog(scope.row)">编辑</el-link>
              <el-link type="primary" @click="del(scope.row)" :disabled="scope.row.filesCount > 0">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-handler" v-if="tableData.length > 0">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.startPage" :page-sizes="pageInfo.pageSizeArr" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total"></el-pagination>
        </div>
      </div>
    </div>

    <!--    添加地域-->
    <el-dialog :title="this.editId > 0 ? '编辑' : '添加类目'" :visible.sync="addDialogVisible" center>
      <el-form :validate-on-rule-change="false" :inline-message="true" ref="userInfoForm" class="user-info-form" label-width="150px">
        <el-form-item label="分类名称：" prop="name">
          <el-input v-model.trim="addName" placeholder="请输入类目名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toAdd">保 存</el-button>
        <el-button type="primary" plain @click="addDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import md5 from 'js-md5'

export default {
  name: 'Project_list',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      tableData: [],
      searchName: '',
      showScreening: true,
      addDialogVisible: false,
      addName: '',
      editId: 0,

      parentId: 0,
      rolePath: {},
      pageInfo: {
        total: 0,
        pageSizeArr: [10, 20, 30, 40],
        size: 10, //分页页面数量大小
        startPage: 1, //开始页码
      },
    }
  },
  mounted() {
    this.parentId = this.$route.query.parentId || 0
    this.searchName = this.$route.query.searchName || ''
    let obj = this.$route.query.rolePath || ''
    if ((!this.searchName && this.parentId <= 0) || obj.length <= 0) {
      this.$message.error('路由错误')
      this.$router.back()
    }
    try {
      this.rolePath = JSON.parse(obj)
    } catch (e) {
      this.$message.error('路由错误')
      this.$router.back()
    }

    this.getList()
  },
  //方法集合
  methods: {
    toNext(parentId) {
      // let obj = [
      //   { name: '资质证书信息库', path: 'Qualification_Category_list' },
      //   {
      //     path: `Qualification_Category_second_list?parentId=${parentId}`,
      //   },
      // ]
      let path = this.rolePath
      path.push({
        path: `Qualification_Category_third_list?parentId=${parentId}`,
      })
      path = JSON.stringify(path)
      this.$router.push(
        `Qualification_Category_third_list?parentId=${parentId}&rolePath=${path}`
      )
    },

    del: function(item) {
      this.$confirm(`确定删除【${item.name}】分类吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$axios
          .post(`/api/manage/qualification/deleteCategory`, `id=${item.id}`)
          .then(res => {
            if (res.success) {
              this.$message.success('删除成功')
              this.search()
            } else this.$message.error(res.desc)
          })
      })
    },
    search: function() {
      this.pageInfo = {
        total: 0,
        pageSizeArr: [10, 20, 30, 40],
        size: 10, //分页页面数量大小
        startPage: 1, //开始页码
      }
      this.getList()
    },
    toAdd() {
      if (this.editId == 0) {
        this.$axios
          .post(
            `/api/manage/qualification/addCategory`,
            `name=${this.addName}&parentId=${this.parentId}`
          )
          .then(res => {
            if (res.success) {
              this.$message.success('添加成功')
              this.addDialogVisible = false
              this.getList()
            } else this.$message.error(res.desc)
          })
      } else {
        this.$axios
          .post(
            `/api/manage/qualification/updateCategory`,
            `name=${this.addName}&id=${this.editId}`
          )
          .then(res => {
            if (res.success) {
              this.$message.success('修改成功')
              this.addDialogVisible = false
              this.getList()
            } else this.$message.error(res.desc)
          })
      }
    },
    showAddDialog(item) {
      if (item == null) {
        this.editId = 0
        this.addName = ''
      } else {
        this.editId = item.id
        this.addName = item.name
      }

      this.addDialogVisible = true
    },

    getList: function() {
      let _this = this
      this.$axios
        .post(
          `/api/manage/qualification/getListPage?size=${this.pageInfo.size}&startPage=${this.pageInfo.startPage}`,
          { name: this.searchName, parentId: this.parentId, level: 1 }
        )
        .then(res => {
          if (res.success) {
            _this.pageInfo.total = res.result.total
            _this.pageInfo.size = res.result.pageSize
            _this.tableData = res.result.list
            if (!_this.tableData || _this.tableData.length < 1) {
              _this.$message({
                message: '未搜索到相关数据',
                type: 'error',
                // onClose: function() {
                //   _this.$router.push(`Qualification_Category_list`)
                // },
              })
              return
            }

            if (_this.parentId < 1) {
              // 最外层直接搜索二级类目
              if (_this.tableData.length > 0)
                _this.parentId = _this.tableData[0].parentId
              _this.rolePath.push({
                name: _this.tableData[0].parentOneName,
                path:
                  'Qualification_Category_second_list?parentId=' +
                  _this.parentId,
              })
            } else {
              // 正常跳转
              _this.rolePath[1].name = _this.tableData[0].parentOneName
            }
          } else {
            _this.$message({
              message: res.desc,
              type: 'error',
              // onClose: function() {
              //   _this.$router.push(`Qualification_Category_list`)
              // },
            })
            return
          }
        })
    },

    // 分页数量大小设置
    handleSizeChange(val) {
      this.pageInfo.size = val
      this.pageInfo.startPage = 1
      this.getList()
    },
    // 分页当前页设置
    handleCurrentChange(val) {
      this.pageInfo.startPage = val
      this.getList()
    },
    // 修改table header的背景色
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      return 'background-color: #f5f5f5;color: #333;font-weight: bold;text-align:center'
    },
    // 修改table tr行的样式
    tableCellStyle({ row, column, rowIndex, columnIndex }) {
      return 'text-align:center'
    },
  },
}
</script>


<style lang="less" scoped>
.box-card {
  margin: 30px;
}
.title {
  font-weight: bold;
}

.grey {
  color: #bbb;
}

.red {
  color: red;
}

.screening-box {
  border: 1px solid #ddd;

  .form {
    width: 100%;
  }
}

.screening-head {
  background: #f5f5f5;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .toggle {
    cursor: pointer;
    margin-right: 50px;
  }
}

.screening-body {
  border-top: 1px solid #ddd;
}

.btn-box {
  margin-top: 40px;

  .number {
    color: red;
  }

  .el-button--primary {
    .number {
      color: #fff;
    }
  }
}

.table-title {
  height: 47px;
  line-height: 47px;
  padding: 0 15px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .filing-button {
    margin-left: 30px;
  }
}

.table-box {
  border: 1px solid #ddd;
  margin-top: 30px;

  .el-tag {
    margin: 2px;
    cursor: default;
  }
}

.table-handler {
  padding: 5px 15px;
  text-align: right;

  .check-all {
    margin-right: 20px;
  }
}

.hasEdit {
  position: absolute;
  top: 0;
  left: 0;

  &::before {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border-top: 30px solid red;
    border-right: 30px solid transparent;
  }

  .text {
    position: absolute;
    top: -3px;
    left: 2px;
    color: #fff;
    font-size: 12px;
  }
}

.user-info-form {
  .el-select,
  .el-input {
    width: 300px;
  }

  .tags {
    width: 450px;
  }

  .el-tag {
    cursor: pointer;
    margin-right: 15px;
  }
}

.tags-dialog {
  .el-tag {
    margin-right: 15px;
    margin-bottom: 15px;

    &:last-child {
      margin-right: 30px;
    }
  }

  .button-new-tag {
    margin-bottom: 15px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
    color: #5aa3e5;
    border-color: #bddaf5;
    background: #eff6fc;

    &:focus,
    &:hover {
      color: #fff;
      background: #5aa3e5;
      border-color: #5aa3e5;
    }
  }

  .input-new-tag {
    width: 90px;
    margin-bottom: 15px;
    vertical-align: bottom;
  }
}

.upload-box {
  display: inline-block;
}
</style>
<style>
.list .el-card__body {
  padding: 0px !important;
}
.list .el-footer {
  padding: 0px;
}

.list .el-footer .el-button {
  margin: 0px;
}
</style>
